package com.exam.master;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.StrictMode;
import android.text.TextUtils;

import androidx.annotation.RequiresApi;

import com.exam.commonbiz.base.BasicApplication;
import com.exam.commonbiz.config.ConfigSP;
import com.exam.commonbiz.net.HostUrl;
import com.exam.commonbiz.net.NetError;
import com.exam.commonbiz.net.NetProvider;
import com.exam.commonbiz.net.RequestHandler;
import com.exam.commonbiz.net.XApi;
import com.exam.commonbiz.push.PushHelper;
import com.exam.commonbiz.util.SharedPreferencesUtils;
import com.umeng.commonsdk.UMConfigure;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author yuexingxing
 * @time 2020/6/10
 */
public class WXApplication extends BasicApplication {

    public static WXApplication mInstance;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();

        initHttpConfig();
        //预初始化
        PushHelper.preInit(this);
        //正式初始化
        initPushSDK();
    }

    /**
     * 初始化推送SDK，在用户隐私政策协议同意后，再做初始化
     */
    private void initPushSDK() {
        /*
         * 当用户同意隐私政策协议时，直接进行初始化；
         * 当用户未同意隐私政策协议时，需等用户同意后，再通过PushHelper.init(...)方法进行初始化。
         */
        String hasRead = SharedPreferencesUtils.getParam(this, ConfigSP.SP_PERSONAL_POLICY, "1").toString();
        if (!TextUtils.isEmpty(hasRead) && PushHelper.isMainProcess(this)) {
            //建议在线程中执行初始化
            new Thread(new Runnable() {
                @Override
                public void run() {
                    PushHelper.init(getApplicationContext());
                }
            }).start();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    public static WXApplication getInstance() {
        if (mInstance == null) {
            mInstance = new WXApplication();
        }
        return mInstance;
    }

    public void initHttpConfig() {

        String host = HostUrl.DEV.HOST;
        XApi.registerDefaultProvider(host, new NetProvider() {

            @Override
            public okhttp3.Interceptor[] configInterceptors() {
                return new okhttp3.Interceptor[0];
            }

            @Override
            public okhttp3.Interceptor[] configNetInterceptors() {
                return new okhttp3.Interceptor[0];
            }

            @Override
            public okhttp3.CookieJar configCookie() {
                return null;
            }

            @Override
            public RequestHandler configHandler() {
                return null;
            }

            @Override
            public long configConnectTimeoutMills() {
                return 0;
            }

            @Override
            public long configReadTimeoutMills() {
                return 0;
            }

            @Override
            public boolean configLogEnable() {
                return false;
            }

            @Override
            public boolean handleError(NetError error) {
                return false;
            }

            @Override
            public boolean dispatchProgressEnable() {
                return false;
            }

            @Override
            public void configHttps(okhttp3.OkHttpClient.Builder builder) {

            }
        });
    }

    //设置字体为默认大小，不随系统字体大小改而改变
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
}
